﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using DispatchAssist.BusinessLogical.BLL;
using System.Web.Services.Protocols;
using DispatchAssist.Model;
using DispatchAssist.BusinessLogical.Model;
using CarolLib;

namespace DispatchAssist.WebService
{
    /// <summary>
    /// AlertMessageGetterService 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://DispatchAssist.ceair.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class AlertMessageGetterService : System.Web.Services.WebService
    {
        #region Members.

        public DASoapHeader headerContext = new DASoapHeader();

        #endregion

        [WebMethod(Description = "根据席位ID获取告警信息列表的Json字符串")]
        [SoapHeader("headerContext", Direction = SoapHeaderDirection.In)]
        [Validation(EnableLog = false)]
        public string GetJsonAlerMessage(string seatID)
        {
            return ClientInfoCacheHelper.GetJsonClientAlertListBySeat(seatID);
        }

        [WebMethod(Description = "根据席位列表获取告警信息列表的Json字符串")]
        [SoapHeader("headerContext", Direction = SoapHeaderDirection.In)]
        [Validation(EnableLog = false)]
        public string GetJsonAlerMessageBySeatList()
        {
            return ClientInfoCacheHelper.GetJsonClientAlertListBySeat(headerContext.SelfSeatID, headerContext.DelegateSeatIDs);
        }

        [WebMethod(Description = "根据AlertID数组、告警方式、用户ID更新告警信息(非短信)状态")]
        [SoapHeader("headerContext", Direction = SoapHeaderDirection.In)]
        [Validation(EnableLog = false)]
        public void UpdateAlertStatus(string[] ids, int actionFlag, string userID)
        {
            new MessageCreateManger().UpdateAlertStatus(ids, actionFlag);

            //获取最新

            ClientInfoCacheHelper.GetNewCacheData();

            //log
            List<ActionPerformLog> list = new List<ActionPerformLog>();

            foreach (string id in ids)
            {
                ActionPerformLog log = new ActionPerformLog();

                log.AlertID = new Guid(id);
                log.UserID = new Guid(userID);
                log.AlertAction = actionFlag;
                log.PerformTime = DateTime.Now;
                log.Description = "";

                list.Add(log);
            }

            ActionLogManager.WriteActionLog(list);

        }

        [WebMethod(Description = "根据AlertID数组、告警方式、用户ID更新告警信息(短信)状态")]
        [SoapHeader("headerContext", Direction = SoapHeaderDirection.In)]
        [Validation(EnableLog = false)]
        public void UpdateSMSAlertStatus(string[] ids, int actionFlag, string userID)
        {
            new MessageCreateManger().UpdateSMSAlertStatus(ids, (Enums.AlertActionFlag)actionFlag);

            //获取最新

            ClientInfoCacheHelper.GetNewCacheData();

            //log
            List<ActionPerformLog> list = new List<ActionPerformLog>();

            foreach (string id in ids)
            {
                ActionPerformLog log = new ActionPerformLog();

                log.AlertID = new Guid(id);
                log.UserID = new Guid(userID);
                log.AlertAction = actionFlag;
                log.PerformTime = DateTime.Now;
                log.Description = "";

                list.Add(log);
            }

            ActionLogManager.WriteActionLog(list);

        }

        [WebMethod(Description = "根据AlertID、飞机号、消息内容以及UserID发送ACARS报文")]
        [SoapHeader("headerContext", Direction = SoapHeaderDirection.In)]
        [Validation(EnableLog = false)]
        public void InesetAcarsMsgModel(string AlertID, string TailNo, string MsgBody, string userID)
        {
            ACARSMsg msg = new ACARSMsg();

            Guid gid = new Guid(AlertID);

            msg.AlertID = gid;
            msg.ReceiveTailNo = TailNo;
            msg.MsgBody = MsgBody;
            msg.MsgStatus = (int)Enums.MessageStatus.FinishCreate;

            new MessageCreateManger().InsertAcarsMsgModelAndUpdateStatus(msg);

            //获取最新

            ClientInfoCacheHelper.GetNewCacheData();

            //log
            ActionLogManager.WriteActionLog(new Guid(AlertID), (int)Enums.AlertActionFlag.ManualSendAcrarsAlert, DateTime.Now, new Guid(userID), "");
        }

        [WebMethod(Description = "根据AlertID、发送组数组、消息内容以及UserID发送ACARS报文")]
        [SoapHeader("headerContext", Direction = SoapHeaderDirection.In)]
        [Validation(EnableLog = false)]
        public void InsertSMSMsgModel(string AlertID, string[] CompanyCodes, string MsgBody, string userID)
        {
            MessageCreateManger manager = new MessageCreateManger();

            int count = 0;

            foreach (string code in CompanyCodes)
            {

                List<SMSContact> contactList = manager.GetContactList(code);

                count++;

                string GroupSN = DateTime.Now.ToString("yyMMddhhmmssfff") + count.ToString();

                foreach (SMSContact contact in contactList)
                {
                    SMSMsg msg = new SMSMsg();

                    msg.SMSAlertID = new Guid(AlertID);
                    msg.GroupSN = decimal.Parse(GroupSN);
                    msg.MobileNo = contact.ContactMobile;
                    msg.MsgBody = MsgBody;
                    msg.MsgStatus = (int)Enums.MessageStatus.FinishCreate;

                    try
                    {
                        manager.InsertSMSModelAndUpdateStatus(msg);
                    }
                    catch (Exception e)
                    {
                        Log.Exception(e);
                        continue;
                    }
                }
            }

            //更新状态

            try
            {
                manager.UpdateSMSAlertStatus(new string[] { AlertID }, Enums.AlertActionFlag.ManualSendSMSAlert);
            }
            catch (Exception e)
            {
                Log.Exception(e);
            }


            //获取最新

            ClientInfoCacheHelper.GetNewCacheData();

            //log
            ActionLogManager.WriteActionLog(new Guid(AlertID), (int)Enums.AlertActionFlag.ManualSendSMSAlert, DateTime.Now, new Guid(userID), "");

        }

        [WebMethod(Description = "更新委托关系的状态")]
        [SoapHeader("headerContext", Direction = SoapHeaderDirection.In)]
        [Validation(EnableLog = false)]
        public void UpdateDelegateStatus(Guid RelationID,int Status)
        {
            new AuthManager().UpdateDelegateStatus(RelationID, Status);
        }

        [WebMethod(Description = "新增委托席位")]
        [SoapHeader("headerContext", Direction = SoapHeaderDirection.In)]
        [Validation(EnableLog = false)]
        public string InsertDelegateSeat(SeatRelation Source)
        {
            return new AuthManager().InsertDelegateRelation(Source);
        }

        [WebMethod(Description = "获取可以委托的席位列表")]
        [SoapHeader("headerContext", Direction = SoapHeaderDirection.In)]
        [Validation(EnableLog = false)]
        public List<Seat> GetDelegatableSeats(Guid SeatID)
        {
            return new AuthManager().GetDelegatableSeats(SeatID);
        }
    
    }
}
